package pers.dawnyang.modular.system.service.impl;

import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import pers.dawnyang.common.annotation.SP;
import pers.dawnyang.common.domain.entity.SysDept;
import pers.dawnyang.modular.system.domain.request.dept.DeptReqR;
import pers.dawnyang.modular.system.domain.response.dept.DeptDtree;
import pers.dawnyang.modular.system.domain.response.dept.DeptRes;
import pers.dawnyang.modular.system.mapper.SysDeptMapper;
import pers.dawnyang.modular.system.service.DeptService;

@Service
public class DeptServiceImpl extends ServiceImpl<SysDeptMapper, SysDept> implements DeptService {

    @Autowired
    private SysDeptMapper sysDeptMapper;

    /*    @Override
    public List<DeptRes> getTreeList(Long pid) {
        List<DeptRes> list = sysDeptMapper.getDataByPid(pid);
        if (list == null || list.size() == 0) {
            return list;
        }
        for (DeptRes deptRes : list) {
            Long deptId = deptRes.getDeptId();
            List<DeptRes> children = getTreeList(deptId);
            if (children != null && children.size() > 0) {
                deptRes.setTreeList(children);
            }
        }
        return list;
    }*/


    @Override
    public List<DeptDtree> getDtreeList() {
        return sysDeptMapper.getDtreeList();
    }

    @SP
    @Override
    public Page<DeptRes> getTreePage(Page<DeptRes> page, DeptReqR req) {
	Page<DeptRes> resPage = sysDeptMapper.getTreePage(page, req);
	List<DeptRes> records = resPage.getRecords();
	List<DeptRes> newResList = records.stream().sorted(Comparator.comparing(DeptRes::getDeptId).reversed()).collect(Collectors.toList());
	resPage.setRecords(newResList);
	return resPage;
    }

    @Override
    public List<DeptRes> getList(DeptReqR req) {
	return sysDeptMapper.getList(req);
    }

}
